﻿//-----------------------------------------------------------------------
// <copyright file="SpeakViewModel.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 

namespace ChatBox.UI.Windows.Main
{
    using System.Windows;
    using System.Windows.Input;

    using ChatBox.UI.ViewModels;
    using ChatBox.Utility;

    /// <summary>
    /// View model for part of the main window to do with sending text
    /// </summary>
    public class SpeakViewModel : BaseViewModel
    {
        #region data

        /// <summary>
        /// Command to retrieve the last message
        /// </summary>
        private readonly ICommand retrieveLastMessageCommand;

        /// <summary>
        /// Speak area animated visiblity for binding
        /// </summary>
        private VisibilityAnimationState speakVisibilityState;

        /// <summary>
        /// Speak area visiblity for binding
        /// </summary>
        private Visibility speakVisibility;

        /// <summary>
        /// Speak area visiblity for binding
        /// </summary>
        private bool isSpeakVisible;

        /// <summary>
        /// The text of the message being typed
        /// </summary>
        private string speakMessageText;

        /// <summary>
        /// Font size for the speak area
        /// </summary>
        private double speakFontSize;

        /// <summary>
        /// The last message sent
        /// </summary>
        private string lastMessage;

        /// <summary>
        /// Flag for if there is a last message to retreive
        /// </summary>
        private bool canRetrieveLastMessage;

        /// <summary>
        /// Flag for if there is text to be spoken
        /// </summary>
        private bool canSpeak;

        /// <summary>
        /// Use the built-in spelling checker
        /// </summary>
        private bool useBuiltInSpellchecker;

        #endregion

        /// <summary>
        /// Initializes a new instance of the SpeakViewModel class
        /// </summary>
        /// <param name="retrieveLastMessageCommand">the command to retrieve the last message</param>
        public SpeakViewModel(RetrieveLastMessageCommand retrieveLastMessageCommand)
        {
            this.retrieveLastMessageCommand = retrieveLastMessageCommand;
        }

        #region properties with notify

        /// <summary>
        /// Gets or sets the visiblity of the speak area
        /// </summary>
        public VisibilityAnimationState SpeakVisibilityState
        {
            get
            {
                return this.speakVisibilityState;
            }

            set
            {
                if (this.speakVisibilityState != value)
                {
                    this.speakVisibilityState = value;
                    OnPropertyChanged("SpeakVisibilityState");

                    this.SpeakVisibility = value.ToVisibility();
                    this.IsSpeakVisible = value.IsVisibleState();
                }
            }
        }

        /// <summary>
        /// Gets or sets the visiblity of the speak area
        /// </summary>
        public Visibility SpeakVisibility
        {
            get
            {
                return this.speakVisibility;
            }

            set
            {
                if (this.speakVisibility != value)
                {
                    this.speakVisibility = value;
                    OnPropertyChanged("SpeakVisibility");
                }
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the speak area is visible
        /// </summary>
        public bool IsSpeakVisible
        {
            get
            {
                return this.isSpeakVisible;
            }

            set
            {
                if (this.isSpeakVisible != value)
                {
                    this.isSpeakVisible = value;
                    OnPropertyChanged("IsSpeakVisible");

                    if (value)
                    {
                        if (!this.SpeakVisibilityState.IsVisibleState())
                        {
                            this.SpeakVisibilityState = VisibilityAnimationState.Showing;
                        }
                    }
                    else
                    {
                        if (this.SpeakVisibilityState.IsVisibleState())
                        {
                            this.SpeakVisibilityState = VisibilityAnimationState.Hiding;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether to use the built-in spelling checker
        /// </summary>
        public bool UseBuiltInSpellchecker
        {
            get
            {
                return this.useBuiltInSpellchecker;
            }

            set
            {
                if (this.useBuiltInSpellchecker != value)
                {
                    this.useBuiltInSpellchecker = value;
                    OnPropertyChanged("UseBuiltInSpellchecker");
                }
            }
        }

        /// <summary>
        /// Gets or sets the font size for the speak area
        /// </summary>
        public double SpeakFontSize
        {
            get
            {
                return this.speakFontSize;
            }

            set
            {
                if (this.speakFontSize != value)
                {
                    this.speakFontSize = value;
                    OnPropertyChanged("SpeakFontSize");
                }
            }
        }

        /// <summary>
        /// Gets or sets the text of the message being typed
        /// </summary>
        public string SpeakMessageText
        {
            get
            {
                return this.speakMessageText;
            }

            set
            {
                if (this.speakMessageText != value)
                {
                    this.speakMessageText = value;
                    OnPropertyChanged("SpeakMessageText");

                    this.CanSpeak = value.HasContent();
                }
            }
        }

        /// <summary>
        /// Gets a value indicating whether there is a last message to retreive
        /// </summary>
        public bool CanRetrieveLastMessage
        {
            get
            {
                return this.canRetrieveLastMessage;
            }

            private set
            {
                if (this.canRetrieveLastMessage != value)
                {
                    this.canRetrieveLastMessage = value;
                    OnPropertyChanged("CanRetrieveLastMessage");
                }
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether there is text to be spoken
        /// </summary>
        public bool CanSpeak
        {
            get
            {
                return this.canSpeak;
            }

            set
            {
                if (this.canSpeak != value)
                {
                    this.canSpeak = value;
                    OnPropertyChanged("CanSpeak");
                }
            }
        }

        #endregion

        /// <summary>
        /// Gets or sets the last message sent. Kept in case the user wants to resend it
        /// </summary>
        public string LastMessage
        {
            get
            {
                return this.lastMessage;
            }

            set
            {
                this.lastMessage = value;
                this.CanRetrieveLastMessage = value.HasContent();
            }
        }

        /// <summary>
        /// Gets the command to retrieve the last message
        /// </summary>
        public ICommand RetrieveLastMessageCommand
        {
            get
            {
                return this.retrieveLastMessageCommand;
            }
        }
    }
}
